package com.leon.programPeral.ch3.ext1;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Setter
@Getter
public class Ch3Ext1PersonTaxClient
{
	private static List<TaxRate> rates = new ArrayList<>();

	static
	{
		// 这里是大量的初始化操作，设置每个税率的收入值与速算扣除数，这里仅用三个做区分
		rates.add(new TaxRate(0.17f, 4200, 50));
		rates.add(new TaxRate(0.16f, 3700, 15));
		rates.add(new TaxRate(0.15f, 3200, 5));
		rates.add(new TaxRate(0.14f, 2700, 0));
		rates.add(new TaxRate(0, 0, 0));

	}

	public static void main(String[] args)
	{
		int income = 3700;
		for (TaxRate rate : rates)
		{
			if (income > rate.getLowIncome())
			{
				System.out.println(cal(income, rate));
				break;
			}
		}
	}

	private static int cal(int income, TaxRate rate)
	{
		if (income > rate.getLowIncome())
		{
			return (int) (rate.getRate() * (income - 2700) - rate.getFastCal());
		}
		return -1;
	}
}

/**
 * 收入税率表
 */
@Setter
@Getter
class TaxRate
{
	public TaxRate(float rate, int lowIncome, int fastCal)
	{
		this.rate = rate;
		this.lowIncome = lowIncome;
		this.fastCal = fastCal;
	}

	// 税率
	private float rate;
	// 适用此税率的最低收入值
	private int lowIncome;
	// 此税率的速算扣除数
	private int fastCal;
}